package cc.jinglupeng.kom.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by jinglupeng on 4/10/16.
 */
public class KomConfig {

    private static Logger logger = LoggerFactory.getLogger(KomConfig.class);
    private static Map<String, String> config = new HashMap<>();

    private static int getInt(String key, int defaultValue) {
        try {
            String str = KomConfig.get(key);
            if (str != null) {
                str = str.trim();
                int value = Integer.parseInt(str);
                return value;
            }
        } catch (Exception e) {
            logger.error("读取参数[{}]时发生异常!", key, e);
        }
        return defaultValue;
    }

    private static String get(String key) {
        String value = config.get(key);
        if (value != null) value = value.trim();
        if (value == null) {
            logger.warn("config[{}] not found!", key);
        }
        return value;
    }

    /**
     * 获取Zk 地址
     *
     * @return
     */
    public static String[] getZkConnectStrings() {
        String hostPorts = get("zk.hostPorts");
        if (hostPorts != null) {
            return hostPorts.split(",");
        }
        logger.error("未配置Zk链接参数");
        return null;
    }

    /**
     * 获取Zk 超时时间
     *
     * @return
     */
    public static int getZkSessionTimeout() {
        return getInt("zk.timeout", 3000);
    }

    public static void put(String key, String value) {
        config.put(key, value);
    }

}
